package com.topscomm.cp.controller;

import com.topscomm.cp.CpController;
import com.topscomm.cp.CpSystemConst;
import com.topscomm.cp.pojo.CpCustomerCompanyOtherEntity;
import com.topscomm.cp.service.ICpCustomerCompanyOtherService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: xinfushuo
 * @date: 2021-11-04
 * @description:企业客户非电Controller
 * @modify:
 */
@RestController
@RequestMapping("cp/customerCompanyOther")
public class CpCustomerCompanyOtherController extends CpController<CpCustomerCompanyOtherEntity, ICpCustomerCompanyOtherService> {
@Override
    protected String spellRefListSql(Map<String, Object> paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" enabled=1 ");
        if (paraMap.containsKey("qry_name")) {
            String value = paraMap.get("qry_name").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and name like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_nature")) {
            String value = paraMap.get("qry_nature").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and nature = '{0}' ", value);
        }
        if (paraMap.containsKey("province")) {
            String value = paraMap.get("province").toString();
            sql.appendSingle("and (");
            if (!StringUtil.isEmpty(value)) {
                String[] deptcode = value.split(",");
                for (String code : deptcode) {
                    sql.appendSingle("deptcode like '{0}%' or ", code);
                }
                sql = sql.substring(0, sql.length() - 3);
                sql.appendSingle(")");
            }
        }
        if (paraMap.containsKey("qry_deptname")) {
            String value = paraMap.get("qry_deptname").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and deptname like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_relationlvl")) {
            String value = paraMap.get("qry_relationlvl").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and relationlvl = '{0}' ", value);
        }
        sql.appendSingle(" and state = '{0}' ", CpSystemConst.CpCustomerStateEnum.Publish);
        sql.appendSingle(this.service.appendChooseAuthSql(paraMap,getUserBean()));
        return sql.toString();
    }
    /**
     * @description:查询参照列表
     * @param paraMap
     * @return
     * @author: donghang
     * @date: 2019年8月8日下午6:06:52
     * @modify:
     */
    @Override
    @GetMapping(value = "/refList")
    public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
        try {
            paraMap = BaseUtil.decodeSecureMap(paraMap);
            Query query = new Query(paraMap);
            String sqlWhere = this.spellRefListSql(paraMap);
            int totalCount = this.service.getCount(sqlWhere);
            List<Map<String, Object>> tempList = null;
            if (query.getPageSize() > 0) {
                tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
                        query.getSidx(), query.getSord());
            } else
                tempList = this.service.queryMapByWhere(sqlWhere);
            tempList.forEach(item->item.put("customersourcetype", CpCustomerCompanyOtherEntity.tableName));
            Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
            return ResponseResult.ok(page);
        } catch (Exception ex) {
            return handleControllerException(ex);
        }
    }
    @GetMapping({"/BillInfo"})
    public ResponseResult<Object> BillInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
        paraMap = BaseUtil.decodeSecureMap(paraMap);
        Map<String,Object> data = new HashMap<>();
        if (paraMap.containsKey("qry_customercode")) {
            String customercode = paraMap.get("qry_customercode").toString();
            List<Map<String, Object>> billInfos = this.service.queryMapByWhere(" customercode='" + customercode + "'", "queryBillInfoMapByWhere");
            for(Map<String, Object> infoItem : billInfos){
                infoItem.put("customercompanyaddress", ConvertUtil.convertToString(infoItem.get("addressprovince"))
                        + ConvertUtil.convertToString(infoItem.get("addresscity"))
                        + ConvertUtil.convertToString(infoItem.get("addressdistrict"))
                        + ConvertUtil.convertToString(infoItem.get("address")));
            }
            data.put("billlist", billInfos);
            return ResponseResult.ok(data);
        }else{
            return ResponseResult.error("参数中需包含qry_customercode");
        }

    }

}
